const { Sequelize } = require('sequelize');

// 数据库同步配置
const syncConfig = {
  // 是否在启动时同步数据库
  enableSync: process.env.DB_SYNC === 'true' ,
  
  // 同步模式: 'force' | 'alter' | false
  // force: 删除表并重新创建（会丢失数据）
  // alter: 修改表结构（保留数据）
  // false: 不同步
  syncMode: process.env.DB_SYNC_MODE || 'alter',
  
  // 是否在同步时记录日志
  syncLogging: process.env.DB_SYNC_LOGGING === 'true' 
};

const sequelize = new Sequelize(
  process.env.DB_NAME || 'wanyue_education',
  process.env.DB_USER || 'root',
  process.env.DB_PASSWORD || 'password',
  {
    host: process.env.DB_HOST || 'localhost',
    port: process.env.DB_PORT || 3306,
    dialect: process.env.DB_DIALECT || 'mysql',
    logging: process.env.NODE_ENV === 'development' ? console.log : false,
    pool: {
      max: 20,
      min: 0,
      acquire: 30000,
      idle: 10000
    },
    timezone: '+08:00',
    define: {
      timestamps: true,
      underscored: true,
      freezeTableName: true
    }
  }
);

module.exports = { sequelize, syncConfig }; 